<template>
  <!--  <div class="login" :style="'background-image:url('+ Background +');'">-->
  <div class="login" style="background-color: #3A71A8">
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" label-position="left" label-width="0px" class="login-form">
      <h3 class="title">
        法律之家 用户注册
      </h3>
      <el-form-item prop="username">
        <el-input v-model="loginForm.username" type="text" auto-complete="off" placeholder="登录账号">
          <svg-icon slot="prefix" icon-class="edit" class="el-input__icon input-icon" />
        </el-input>
      </el-form-item>
      <el-form-item prop="nickname">
        <el-input v-model="loginForm.nickname" type="text" auto-complete="off" placeholder="真实姓名">
          <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input v-model="loginForm.password" type="password" auto-complete="off" placeholder="密码">
          <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
        </el-input>
      </el-form-item>
      <el-form-item prop="repassword">
        <el-input v-model="loginForm.repassword" type="password" auto-complete="off" placeholder="重复密码">
          <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
        </el-input>
      </el-form-item>
      <el-form-item prop="phone">
        <el-input v-model="loginForm.phone" type="text" auto-complete="off" placeholder="手机号码">
          <svg-icon slot="prefix" icon-class="phone" class="el-input__icon input-icon" />
        </el-input>
      </el-form-item>
      <el-form-item prop="code">
        <el-input v-model="loginForm.code" auto-complete="off" placeholder="验证码" style="width: 63%">
          <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
        </el-input>
        <div class="login-code">
          <img :src="codeUrl" @click="getCode">
        </div>
      </el-form-item>
      <el-form-item>
        <el-button :loading="loading" size="medium" type="primary" style="width:100%;" @click="doRegister">
          <span>注 册</span>
        </el-button>
      </el-form-item>
      <el-form-item>
        <el-button :loading="loading" size="medium" type="info" style="width:100%;" @click="toLogin">
          <span>已有账号，去登录</span>
        </el-button>
      </el-form-item>
    </el-form>
    <!--  底部  -->
<!--    <div v-if="$store.state.settings.showFooter" id="el-login-footer">-->
<!--      <span v-html="$store.state.settings.footerTxt" />-->
<!--      <span> ⋅ </span>-->
<!--      <a href="#" target="_blank">{{ $store.state.settings.caseNumber }}</a>-->
<!--    </div>-->

    <el-dialog
      title="提示"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose">
      <span>账号注册成功，等待管理员审核激活后方可使用！</span>
      <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
  </span>
    </el-dialog>
  </div>
</template>

<script>
import { encrypt } from '@/utils/rsaEncrypt'
import { getCodeImg } from '@/api/login'
import { isvalidPhone } from '@/utils/validate'
import { register } from '@/api/system/user'
import Background from '@/assets/images/background.jpg'
export default {
  data() {
    // 自定义验证
    const validPhone = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请输入电话号码'))
      } else if (!isvalidPhone(value)) {
        callback(new Error('请输入正确的11位手机号码'))
      } else {
        callback()
      }
    }
    return {
      dialogVisible: false,
      Background: Background,
      codeUrl: '',
      cookiePass: '',
      loginForm: {
        username: '',
        nickname: '',
        password: '',
        repassword: '',
        phone: '',
        code: '',
        uuid: ''
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', message: '登录账号不能为空' }],
        nickname: [{ required: true, trigger: 'blur', message: '真实姓名不能为空' }],
        password: [{ required: true, trigger: 'blur', message: '密码不能为空' }],
        repassword: [{ required: true, trigger: 'blur', message: '密码不能为空' }],
        phone: [{ required: true, trigger: 'blur', validator: validPhone }],
        code: [{ required: true, trigger: 'change', message: '验证码不能为空' }]
      },
      loading: false,
      redirect: undefined
    }
  },
  watch: {
    // $route: {
    //   handler: function(route) {
    //     this.redirect = route.query && route.query.redirect
    //   },
    //   immediate: true
    // }
  },
  created() {
    // 获取验证码
    this.getCode()
    // 获取用户名密码等Cookie
    // this.getCookie()
    // token 过期提示
  },
  methods: {
    toLogin() {
      this.$router.push('/login')
    },
    doRegister() {
      this.$refs.loginForm.validate(valid => {
        if (this.loginForm.password !== this.loginForm.repassword) {
          const h = this.$createElement;

          this.$notify({
            title: '提示',
            message: h('i', { style: 'color: teal' }, '两次输入的密码不一致！')
          })
          valid = false
        }
        const registerUser = {
          username: this.loginForm.username,
          nickname: this.loginForm.nickname,
          password: encrypt(this.loginForm.password),
          code: this.loginForm.code,
          phone: this.loginForm.phone,
          uuid: this.loginForm.uuid
        }
        if (valid) {
          register(registerUser).then(res => {
            console.log('res=======', res)
            this.dialogVisible = true
            this.$refs['loginForm'].resetFields()
          })
        }
      })
    },
    getCode() {
      getCodeImg().then(res => {
        this.codeUrl = res.img
        this.loginForm.uuid = res.uuid
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">
  .login {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100%;
    background-size: cover;
  }
  .title {
    margin: 0 auto 30px auto;
    text-align: center;
    color: #707070;
  }

  .login-form {
    border-radius: 6px;
    background: #ffffff;
    width: 385px;
    padding: 25px 25px 5px 25px;
    .el-input {
      height: 38px;
      input {
        height: 38px;
      }
    }
    .input-icon{
      height: 39px;width: 14px;margin-left: 2px;
    }
  }
  .login-tip {
    font-size: 13px;
    text-align: center;
    color: #bfbfbf;
  }
  .login-code {
    width: 33%;
    display: inline-block;
    height: 38px;
    float: right;
    img{
      cursor: pointer;
      vertical-align:middle
    }
  }
</style>
